package com.xmcowell.sportpacemaster.views;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.xmcowell.sportpacemaster.R;
import com.xmcowell.sportpacemaster.config.Contents;
import com.xmcowell.sportpacemaster.config.Type;
import com.xmcowell.sportpacemaster.layout.MainActivity;

import java.util.List;

/**
 * Created by Administrator on 2017/6/8 0008.
 */

public class MainLeftPopupView extends PopupWindow implements View.OnClickListener {
    public List<String> mData;
    public Context mContext;
    public View mView;
    public int currentWidth;
    public int currentHeight;
    private int num;

    private int tvW = 160;//全部textview的宽度与总宽度的差值，930为160
    private int tvSize = 35;//textview的字体大小，930为30
    private int tvpadding = 4;

    public MainLeftPopupView(Context context, int width, int height, List<String> list) {
        super(width, height);
        mContext = context;
        mData = list;
        currentWidth = width;
//        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        mView = mInflater.inflate(R.layout.popup_main_left,null);
        LinearLayout layout = new LinearLayout(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.setLayoutParams(params);
        layout.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL);
        layout.setOrientation(LinearLayout.HORIZONTAL);
//        layout.setBackgroundColor(mContext.getResources().getColor(R.color.bg_line1));
        layout.setBackground(mContext.getResources().getDrawable(R.mipmap.bg_main_left));
        initValue();
        initText(layout, width);

        //设置view
        this.setContentView(layout);
        //设置宽度
        this.setWidth(width);
        //设置高度
        this.setHeight(height);
        //设置弹出框可点击
        this.setFocusable(true);
        //设置弹出框可触摸
        this.setTouchable(true);
        //实例化一个弹窗背景
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置弹窗背景
        this.setBackgroundDrawable(dw);


        layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        currentWidth = layout.getMeasuredWidth();
        currentHeight = layout.getMeasuredHeight();
    }

    private void initValue() {
        if (Contents.SPORT_TYPE.equals(Type.PM810)){
            tvW = 160;//
            tvSize = 35;
        }else if (Contents.SPORT_TYPE.equals(Type.PM820)){
            tvW = 160;//
            tvSize = 35;
        }else if (Contents.SPORT_TYPE.equals(Type.PM930)){
            tvW = 160;//
            tvSize = 35;
        }else{//940
            if (Contents.isTTYS3){
                tvW = 200;//
                tvSize = 25;
            }else{
                tvW = 200;//
                tvSize = 35;
            }

        }
    }

    private void initText(LinearLayout layout, int width) {
        int tvWidth = (width - tvW) / mData.size();
//        Log.e("MainLeftPopupView", "tv的宽度  " + tvWidth);
        for (int i = 0; i < mData.size(); i++) {
            TextView tv = new TextView(mContext);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(tvWidth, tvWidth);
            if (Contents.SPORT_TYPE.equals(Type.PM810)){
                params.setMargins(tvpadding, tvpadding,tvpadding, 2);
            }else{
                params.setMargins(tvpadding, tvpadding,tvpadding, tvpadding);
            }

            tv.setLayoutParams(params);
            tv.setBackgroundResource(R.drawable.bg_main_num);
            tv.setText(mData.get(i) + "");
            tv.setTextColor(Color.WHITE);
            tv.setTextSize(tvSize);
            tv.setGravity(Gravity.CENTER);
            tv.setOnClickListener(this);
            layout.addView(tv);
        }
    }

    @Override
    public void onClick(View view) {
        TextView tv = (TextView) view;
        MainActivity.instance.setPopupSlope(tv.getText().toString());
//        Log.e("MainLeftPopupView", "点击的值为  " + tv.getText().toString());
    }

    public int getCurrentWidth() {
        return currentWidth;
    }
    public int getCurrentHeight(){
        return currentHeight;
    }

    @Override
    public void showAsDropDown(View anchor) {
//        int x = 0;
//        int y = 0;
//        int[] location = new int[2];
//        // 获得位置
//        anchor.getLocationOnScreen(location);
//        x = (location[0] + anchor.getWidth()) - currentWidth / 2;
//        y = location[1] - currentHeight;
//        showAtLocation(anchor, Gravity.NO_GRAVITY, x, y);
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
    }

}
